<?php
/**
 * Protoweb's base controller with base functionality
 */
include_once 'pwcontroller.php';
include_once 'projects.php';

/**
 * The controller action model
 *
 * Public controller methods are friendly urls thanks to {@link http://codeigniter.com
 * Codeigniter}.
 *
 * @author Gonzalez Federico, Viera Leonel
 * @package Protoweb
 * @subpackage controller
 */
class Actions extends PwController 
{
    public function __construct()
    {
        parent::__construct();
    }

    public function create($entityID)
    {
        // Checking permissions user
        $data['entity']  = new Entity($entityID);
        $data['user']    = Projects::security($data['entity']->project->id, "member");

        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $data = $this->create_save($entityID);
        }

        $data['project'] = new Project($data['entity']->project->id);
        $data['templates'] = Template::getAll();

        $this->smartyview->render('action_create.html', $data);
    }

    private function create_save($entityID) 
    {
        $data['entity']     = new Entity($entityID);
        $data['user']       = Projects::security($data['entity']->project->id, "member");
        $data['actionName'] = $this->input->post('name');
        $template           = new Template($this->input->post('template'));

        $this->smartyview->smarty->assign($data);
        $method  = $this->smartyview->smarty->fetch($template->getFilePath());

        ////////////////////////// WORKAROUND ///////////////////
        $controller = file_get_contents($data['entity']->customControllerFilePath());
        $insertArea = explode('{', $controller);
        $insertArea = $insertArea[1];
        $controller = str_replace($insertArea, "\r\n{$method}\r\n" . $insertArea, $controller);
        file_put_contents($data['entity']->customControllerFilePath(), $controller);
        ///////////////////////////////////////////////////////////////////////

        redirect(base_url() . 'actions/edit/' . $data['entity']->id . '/' . $data['actionName'] . '/');
    }

    public function edit($entityID, $actionName) 
    {
        // Checking permissions user
        $data['entity']  = new Entity($entityID);
        $data['user']    = Projects::security($data['entity']->project->id, "member");
        $data['project'] = new Project($data['entity']->project->id);
        $data['loadUrl'] = base_url() . 'actions/get_controller/' . $data['entity']->id . '/';
        $data['saveUrl'] = base_url() . 'actions/save_controller/' . $data['entity']->id . '/';

        try {
            $controller = $data['entity']->getReflectedController();
            if (is_array($controller)) {
                $data['startLine'] = 1;
                $data['error'] = $controller['output'];
            }
            else {
                $data['startLine'] = $controller->getMethod($actionName)->getStartLine() - 1;
            }
        }
        catch(Exception $exception) {
            $data['startLine'] = 1;
        }

        $this->smartyview->render('online_editor.html', $data);
    }

    public function get_controller($entityID) 
    {
        $entity = new Entity($entityID);
        die(file_get_contents($entity->customControllerFilePath()));
    }

    public function save_controller($entityID) 
    {
        $entity  = new Entity($entityID);
        file_put_contents($entity->customControllerFilePath(), $this->input->post('content'));
        die(true);
    }
  
} // class Actions

/* End of file actions.php */
/* Location: ./application/controllers/actions.php */